package com.liferay.admin.quangcao;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.documentlibrary.model.DLFileEntry;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.mto.quangcao.model.QuangCao_banner;
import com.mto.quangcao.model.impl.QuangCao_bannerImpl;
import com.mto.quangcao.service.QuangCao_bannerLocalServiceUtil;
import com.mto.quangcao.service.persistence.QuangCao_bannerFinder;
import com.mto.quangcao.service.persistence.QuangCao_bannerFinderUtil;

/**
 * Portlet implementation class QuangCao-admin
 */
public class QuangCao_admin extends MVCPortlet {

	public void editQuangcao(ActionRequest actionRequest,
			ActionResponse actionResponse) {

		try {
			// new defaul
			ThemeDisplay themedisplay = (ThemeDisplay) actionRequest
					.getAttribute(WebKeys.THEME_DISPLAY);
			ServiceContext serviceContext = ServiceContextFactory.getInstance(
					DLFileEntry.class.getName(), actionRequest);
			UploadPortletRequest uploadPortletRequest = PortalUtil
					.getUploadPortletRequest(actionRequest);
			long quangCaoId = 1;
			quangCaoId = ParamUtil.getLong(actionRequest, "quangcaoId");
			System.out.println("QuangCaoID :" + quangCaoId);
			QuangCao_banner quangcao = QuangCao_bannerLocalServiceUtil
					.getQuangCao_banner(quangCaoId);
			// get value

			QuangCao_banner cd = new QuangCao_bannerImpl();

			String content = quangcao.getContent();
			long countView = quangcao.getCountView();
			long countClick = quangcao.getCountClick();
			long limitClick =ParamUtil.getLong(uploadPortletRequest,
					"limitClick");;
			String userId = uploadPortletRequest.getParameter("author");
			
			Date startDate = quangcao.getStartDate();
			String startDate_ = startDate.toString();
			startDate_ = ParamUtil.getString(uploadPortletRequest,
					"startDate");
			
			Date endDate = quangcao.getEndDate();
			String endDate_ = endDate.toString();
			endDate_ = ParamUtil.getString(uploadPortletRequest,
					"endDate");

			String title = ParamUtil.getString(uploadPortletRequest, "title");
			String destription = ParamUtil.getString(uploadPortletRequest,
					"destription");

			File file = (File) uploadPortletRequest.getFile("fileName");
			long fileEntryId = 0;

			String nameFolder = "newFolder";
			nameFolder = uploadPortletRequest.getParameter("folder");
			System.out.println("nameFolder :" + nameFolder);
			if (file.exists()) {
				fileEntryId = QuangCao_bannerLocalServiceUtil.UploadFile(
						nameFolder, uploadPortletRequest, themedisplay,
						serviceContext);
			} else {
				fileEntryId = quangcao.getFileEntryId();
			}
			String status = quangcao.getStatus();
			String type = uploadPortletRequest.getParameter("type");

			Date createDate = new Date();
			String link = ParamUtil.getString(uploadPortletRequest, "link");

			long userModifyId = themedisplay.getUserId();

			String userModifyIp = uploadPortletRequest
					.getHeader("x-forwarded-for");
			if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest
						.getHeader("Proxy-Client-IP");
			} else if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest
						.getHeader("WL-Proxy-Client-IP");
			} else if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest.getRemoteAddr();
			} else {
				userModifyIp = PortalUtil.getOriginalServletRequest(
						uploadPortletRequest).getRemoteAddr();
			}
			long widthImage =quangcao.getWidthBanner() ;
			long heightImage =quangcao.getHeightBanner();
			widthImage =Long.parseLong(ParamUtil.getString(uploadPortletRequest,
					"widthImage")); 
			heightImage =Long.parseLong(ParamUtil.getString(uploadPortletRequest,
					"heightImage"));
			
			// set value
			cd.setQuangCaoId(quangCaoId);
			cd.setContent(content);
			cd.setCountView(countView);
			cd.setCountClick(countClick);
			cd.setLimitClick(limitClick);
			cd.setUserId(Long.parseLong(userId));
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			cd.setStartDate(formatter.parse(startDate_));
			cd.setEndDate(formatter.parse(endDate_));
			cd.setTitle(title);
			cd.setDescription(destription);
			cd.setFileEntryId(fileEntryId);
			cd.setStatus(status);
			cd.setType(type);
			cd.setCreateDate(createDate);
			cd.setLink(link);
			cd.setUserModifyId(userModifyId);
			cd.setUserModifyIp(userModifyIp);
			cd.setWidthBanner(widthImage);
			cd.setHeightBanner(heightImage);
			
			System.out.println("quangCaoId" + quangCaoId);
			System.out.println("content" + content);
			System.out.println("countView" + countView);
			System.out.println("countClick" + countClick);
			System.out.println("limitClick" + countClick);
			System.out.println("userId" + userId);
			System.out.println("startDate" + startDate);
			System.out.println("endDate" + endDate);
			System.out.println("title" + title);
			System.out.println("destription" + destription);
			System.out.println("userModifyId" + userModifyId);
			System.out.println("userModifyIp" + userModifyIp);
			System.out.println("widthImage" + widthImage);
			System.out.println("heightImage" + heightImage);

			// submit
			QuangCao_bannerLocalServiceUtil.edit(cd);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void createQuangCao(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			// new defaul

			ThemeDisplay themedisplay = (ThemeDisplay) actionRequest
					.getAttribute(WebKeys.THEME_DISPLAY);
			ServiceContext serviceContext = ServiceContextFactory.getInstance(
					DLFileEntry.class.getName(), actionRequest);
			UploadPortletRequest uploadPortletRequest = PortalUtil
					.getUploadPortletRequest(actionRequest);

			long quangCaoId = 1;
			// get value
			QuangCao_banner cd = new QuangCao_bannerImpl();

			String content = "";
			long countView = 0;
			long countClick = 0;
			long limitClick =0;
			limitClick =ParamUtil.getLong(uploadPortletRequest,
					"limitClick");;
			
			String userId = uploadPortletRequest.getParameter("author");

			
			String startDate ="";
			startDate = ParamUtil.getString(uploadPortletRequest,
					"startDate");
			if(startDate.equals(""))
			{
				startDate= "07-06-2013";
			}
			System.out.println("startDate_ :" + startDate);
			
			
			String endDate = "";
			endDate = ParamUtil.getString(uploadPortletRequest,
					"endDate");
			if(endDate.equals(""))
			{
				endDate="07-06-2013";
			}
			System.out.println("endDate_ :" + endDate);
			
			String title = ParamUtil.getString(uploadPortletRequest, "title");
			String destription = ParamUtil.getString(uploadPortletRequest,
					"destription");

			long fileEntryId = 0;
			String nameFolder = "newFolder";
			nameFolder = uploadPortletRequest.getParameter("folder");
			System.out.println("nameFolder :" + nameFolder);

			fileEntryId = QuangCao_bannerLocalServiceUtil.UploadFile(
					nameFolder, uploadPortletRequest, themedisplay,
					serviceContext);
			System.out.println("fileEntryId :" + fileEntryId);
			
			String status ="publish";
			status = uploadPortletRequest.getParameter("status");
			System.out.println("status :" + status);
			
			String type = uploadPortletRequest.getParameter("type");
			System.out.println("type :" + type);
			
			Date createDate = new Date();
			System.out.println("createDate :" + createDate);
			String link = ParamUtil.getString(uploadPortletRequest, "link");
			System.out.println("link :" + link);
			long userModifyId = themedisplay.getUserId();
			System.out.println("userModifyId :" + userModifyId);
			
			String userModifyIp = uploadPortletRequest
					.getHeader("x-forwarded-for");
			if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest
						.getHeader("Proxy-Client-IP");
			}
			if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest
						.getHeader("WL-Proxy-Client-IP");
			}
			if (userModifyIp == null || userModifyIp.length() == 0
					|| "unknown".equalsIgnoreCase(userModifyIp)) {
				userModifyIp = uploadPortletRequest.getRemoteAddr();
			} else {
				userModifyIp = PortalUtil.getOriginalServletRequest(
						uploadPortletRequest).getRemoteAddr();
			}
			System.out.println("userModifyIp :" + userModifyIp);
			
			long widthImage =0 ;
			long heightImage =0;
			
			widthImage =ParamUtil.getLong(uploadPortletRequest,
					"widthImage"); 
			heightImage =ParamUtil.getLong(uploadPortletRequest,
					"heightImage");
			
			System.out.println("widthImage" + widthImage);
			System.out.println("heightImage" + heightImage);
			
			// set value
			cd.setQuangCaoId(quangCaoId);
			cd.setContent(content);
			cd.setCountView(countView);
			cd.setCountClick(countClick);
			cd.setLimitClick(limitClick);
			cd.setUserId(Long.parseLong(userId));
			SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
			cd.setStartDate(formatter.parse(startDate));
			cd.setEndDate(formatter.parse(endDate));
			cd.setTitle(title);
			cd.setDescription(destription);
			cd.setFileEntryId(fileEntryId);
			cd.setStatus(status);
			cd.setType(type);
			cd.setCreateDate(createDate);
			cd.setLink(link);
			cd.setUserModifyId(userModifyId);
			cd.setUserModifyIp(userModifyIp);
			cd.setWidthBanner(widthImage);
			cd.setHeightBanner(heightImage);
			
			System.out.println("quangCaoId" + quangCaoId);
			System.out.println("content" + content);
			System.out.println("countView" + countView);
			System.out.println("countClick" + countClick);
			System.out.println("limitClick" + countClick);
			System.out.println("userId" + userId);
			System.out.println("startDate" + startDate);
			System.out.println("endDate" + endDate);
			System.out.println("title" + title);
			System.out.println("destription" + destription);
			System.out.println("userModifyId" + userModifyId);
			System.out.println("userModifyIp" + userModifyIp);
			System.out.println("widthImage" + widthImage);
			System.out.println("heightImage" + heightImage);

			// submit
			QuangCao_bannerLocalServiceUtil.create(cd);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void deleteBanner(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			long primkey = ParamUtil.getLong(actionRequest, "primkey");
			QuangCao_bannerLocalServiceUtil.deleteQuangCao_banner(primkey);
			SessionMessages.add(actionRequest, "delete-topic-success");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void publishBanner(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			long primkey = ParamUtil.getLong(actionRequest, "primkey");
			QuangCao_banner qCao_banner = QuangCao_bannerLocalServiceUtil
					.getQuangCao_banner(primkey);
			qCao_banner.setStatus("publish");
			QuangCao_bannerLocalServiceUtil.updateQuangCao_banner(qCao_banner);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void unpublishBanner(ActionRequest actionRequest,
			ActionResponse actionResponse) {
		try {
			long primkey = ParamUtil.getLong(actionRequest, "primkey");
			QuangCao_banner qCao_banner = QuangCao_bannerLocalServiceUtil
					.getQuangCao_banner(primkey);
			qCao_banner.setStatus("approved");
			QuangCao_bannerLocalServiceUtil.updateQuangCao_banner(qCao_banner);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<QuangCao_banner> searchQuangCao(ActionRequest actionRequest , ActionResponse actionResponse)
	{
		UploadPortletRequest uploadPortletRequest = PortalUtil
				.getUploadPortletRequest(actionRequest);
		long quangCaoId = 0 ;	
		quangCaoId = ParamUtil.getLong(uploadPortletRequest,
				"quangCaoId");
		System.out.println("QuangCaoId"+quangCaoId);		
		String type= "";
		type = ParamUtil.getString(uploadPortletRequest, "type");
		String title ="";
		title = ParamUtil.getString(uploadPortletRequest, "Title");
		
		List<QuangCao_banner> listQuangCao  =(List<QuangCao_banner>) QuangCao_bannerLocalServiceUtil.getALL(quangCaoId, type, title);
		if(Validator.isNull(listQuangCao))
		{
			System.out.println("List quang cao null");
		}
		else
		{
			for(QuangCao_banner quangcao:listQuangCao)
			{
				System.out.println("QuangCaoId"+quangcao.getQuangCaoId());
				System.out.println("Content"+quangcao.getContent());
				System.out.println("StartDate"+quangcao.getStartDate());
				System.out.println("EndDate"+quangcao.getEndDate());
				System.out.println("FileEntryId"+quangcao.getFileEntryId());
			}
		}

		return listQuangCao;
	}
	
}
